package com.example.administrator.wallet.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.model.bean.WebSocketOrderHistoryBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/8/30 0030.
 */

public class MarketTransactionRecordsAdapter extends BaseAdapter {

    private Context context;
    private List<WebSocketOrderHistoryBean.ResultBean.RecordsBean> list;
    private MarketTransactionRecordsAdapterViewHolder holder;

    public MarketTransactionRecordsAdapter(Context context, List<WebSocketOrderHistoryBean.ResultBean.RecordsBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            holder = new MarketTransactionRecordsAdapterViewHolder();
            convertView = View.inflate(context, R.layout.item_transaction_records,null);

            holder.tvVolume = (TextView) convertView.findViewById(R.id.tv_volume_transaction_records_item);
            holder.tvNumber = (TextView) convertView.findViewById(R.id.tv_transaction_number_transaction_records_item);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_transaction_price_transaction_records_item);
            holder.tvBuyOrSell = (TextView) convertView.findViewById(R.id.tv_buy_or_sell_color_transaction_records_item);
            holder.tvBuOrSellText = (TextView) convertView.findViewById(R.id.tv_buy_or_sell_text_transaction_records_item);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time_transaction_records_item);

            convertView.setTag(holder);
        } else {
            holder = (MarketTransactionRecordsAdapterViewHolder) convertView.getTag();
        }

        holder.tvVolume.setText(list.get(position).getDeal_money());
        holder.tvNumber.setText(list.get(position).getDeal_stock());
        holder.tvPrice.setText(list.get(position).getPrice());

        if (list.get(position).getType() == 1){
            holder.tvBuOrSellText.setText(context.getResources().getString(R.string.Sell));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.tvBuyOrSell.setBackground(context.getDrawable(R.drawable.backups_sell));
                }
            }
        } else {
            holder.tvBuOrSellText.setText(context.getResources().getString(R.string.Sell));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.tvBuyOrSell.setBackground(context.getDrawable(R.drawable.backups_buy));
                }
            }
        }

        long time = (long) list.get(position).getCtime() * 1000;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        holder.tvTime.setText(simpleDateFormat.format(date));

        return convertView;
    }

    public static class MarketTransactionRecordsAdapterViewHolder{
        TextView tvVolume,tvNumber,tvPrice,tvBuyOrSell,tvBuOrSellText,tvTime;
    }
}
